package com.carpark.ejb.DAO;

import com.carpark.ejb.Model.AccountingdataModel;
import com.carpark.ejb.Model.ClientsModel;
import com.carpark.ejb.Model.ParkingpassesModel;
import com.carpark.ejb.Model.PassesrecordsModel;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Krzysztof
 */
@Stateful
public class PassesrecordDAOImpl implements PassesrecordDAO {

    private EntityManager entityManager;

    public PassesrecordDAOImpl() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
        entityManager = factory.createEntityManager();
    }

    @Override
    public PassesrecordsModel getPassesrecordById(Integer id) {
        return (PassesrecordsModel) entityManager.createNamedQuery("PassesrecordsModel.findByRecordId").setParameter("recordId", id).getSingleResult();
    }

    @Override
    public List<PassesrecordsModel> getAllPassesRecords() {
        return (List<PassesrecordsModel>) entityManager.createNamedQuery("PassesrecordsModel.findAll").getResultList();
    }

    @Override
    public EntityManager getEntityManager() {
        return this.entityManager;
    }

    @Override
    public void addPassesrecord(PassesrecordsModel am) {
        entityManager.getTransaction().begin();
        entityManager.persist(am);
        entityManager.getTransaction().commit();
    }

    @Override
    public int getPassesrecordModelSize() {
        return entityManager.createNamedQuery("PassesrecordsModel.findAll").getResultList().size();
    }

    @Override
    public void removePassesrecord(Integer id) {
        PassesrecordsModel rowToDelete = getPassesrecordById(id);

        entityManager.getTransaction().begin();
        entityManager.remove(rowToDelete);
        entityManager.getTransaction().commit();

        entityManager.remove(rowToDelete);
    }

    @Override
    public void updatePassesrecord(PassesrecordsModel am) {
        PassesrecordsModel rowToUpdate = getPassesrecordById(am.getRecordId());

        entityManager.getTransaction().begin();
        rowToUpdate.setAccountingDataId(am.getAccountingDataId());
        rowToUpdate.setAddingDate(am.getAddingDate());
        rowToUpdate.setClientId(am.getClientId());
        rowToUpdate.setParkingPassId(am.getParkingPassId());

        entityManager.getTransaction().commit();
    }

    @Override
    public List<PassesrecordsModel> getPassesrecordByAddingDate(Date addingDate) {
        return (List<PassesrecordsModel>) entityManager.createNamedQuery("PassesrecordsModel.findByAddingDate").setParameter("addingDate", addingDate).getResultList();

    }

    @Override
    public int getPassesrecordByAddingDateSize(Date addingDate) {
        List<PassesrecordsModel> list = (List<PassesrecordsModel>) entityManager.createNamedQuery("PassesrecordsModel.findByAddingDate").setParameter("addingDate", addingDate).getResultList();
        return list.size();
    }

    @Override
    public List<ParkingpassesModel> getParkingPassesByClient(ClientsModel cl) {
        List<ParkingpassesModel> list = (List<ParkingpassesModel>) entityManager.createNamedQuery("PassesrecordsModel.findByClientId").setParameter("clientId", cl).getResultList();
        return list;
    }

    @Override
    public List<AccountingdataModel> getAccountingDataByClient(ClientsModel cl) {
        return (List<AccountingdataModel>) entityManager.createNamedQuery("PassesrecordsModel.findAccountingByClient").setParameter("clientId", cl).getResultList();
    }

    @Override
    public AccountingdataModel getAccountingByPass(ParkingpassesModel pass) {
        return (AccountingdataModel) entityManager.createNamedQuery("PassesrecordsModel.findAccountingByPass").setParameter("pass", pass).getSingleResult();
    }

    @Override
    public List<ParkingpassesModel> getParkingPassesByAccounting(AccountingdataModel acc) {
         List<ParkingpassesModel> list = (List<ParkingpassesModel>) entityManager.createNamedQuery("PassesrecordsModel.findByAccId").setParameter("acc", acc).getResultList();
        return list;
    }
}
